<?php

extract(extractPost(), EXTR_PREFIX_ALL, 'b');
if(isset($b_name) && isset($b_board_id)){
    
    
    
    $board = R::load('board', $b_board_id);
    
    if($board->id == 0){
        jsonErrorDie("Board with id = $b_board_id cannot be found.");
    }
    $column = R::dispense( 'column' );
    $column->name = $b_name;
    $column->wip = -1;
    if(isset($b_wip)){
        $column->wip = $b_wip;
    }
    
    $priority = 0; // Default - lowest priority
    if(isset($b_priority)){
        $priority = $b_priority;
        if($b_priority == 2){
            $highPriority_exists = R::findOne('column',' board_id = :board_id AND priority = 2', [":board_id" => $b_board_id]);
            if($highPriority_exists){
                jsonErrorDie("Column with high priority already exists in table.");
            }
        }
    }
    
    // Checking for left and right most column
    // $b_boundary: -1 = leftmost, 1 = rightmost, else normal
    if(isset($b_boundary) && abs($b_boundary) == 1){
        $boundary_exists = R::findOne('column',' board_id = :board_id AND boundary = :boundary', [":board_id" => $b_board_id, ":boundary" => $b_boundary]);
        if($boundary_exists){
            jsonErrorDie( (($b_boundary == -1)?"Left":"Right") . "most column is already defined (". $boundary_exists->name .").");
        }
        
    }
    else{
        $b_boundary = 0;
    }
    $column->boundary = $b_boundary;
    $newColumn_exists = R::findOne('column',' board_id = :board_id AND name = :name ', [":board_id" => $b_board_id, ":name" => $b_name]);
    //Adding subcolumn to existing or non existing column
    if(isset($b_subcolumn) && $newColumn_exists){
        
        $deletedColumn = false;
        // Če stolpec obstaja, vendar nima podstolpcev ga zavržemo in dodamo novega
        if($newColumn_exists->subcolumn == NULL){
            $column->position = $newColumn_exists->position;
            $priority = $newColumn_exists->priority;
            $deletedColumn = true;
            R::trash($newColumn_exists);
        }
        $newSubcolumn_exists = R::findOne('column',' board_id = :board_id AND name = :name AND subcolumn = :subcolumn', 
                                    [":board_id" => $b_board_id, ":name" => $b_name, ":subcolumn" => $b_subcolumn]);
        
        if($newSubcolumn_exists){
            jsonErrorDie("Column with provided name already contains provided subcolumn name.");
        }
        if(!$deletedColumn){
            if($b_side == "left"){
                $posi = R::getRow("SELECT `position` FROM `column` WHERE `board_id`=? AND `name` = ? ORDER BY `position` ASC", array($b_board_id, $b_name));
                $column->position=$posi['position']-1; 
            }
            else{
                $posi = R::getRow("SELECT `position` FROM `column` WHERE `board_id`=? AND `name` = ? ORDER BY `position` DESC", array($b_board_id, $b_name));
                $column->position=$posi['position']+1; 
            }
        }
        $column->subcolumn = $b_subcolumn;
    }
    // Adding whole new column
    else{

        if($newColumn_exists){
            jsonErrorDie("Column with provided name already exists.");
        }
        
         if($b_side == "left"){
            $pos = R::getRow("SELECT `position` FROM `column` WHERE `board_id`=? ORDER BY `position` ASC", array($b_board_id));
            if(isset($pos['position'])){
                $column->position=$pos['position']-100; 
            }
            else{
                $column->position=0;
            }
        }
        else{
            $pos = R::getRow("SELECT `position` FROM `column` WHERE `board_id`=? ORDER BY `position` DESC", array($b_board_id));
            if(isset($pos['position'])){
                $column->position=$pos['position']+100; 
            }
            else{
                $column->position=0;
            }
        }
        if(isset($b_subcolumn)){
            $column->subcolumn = $b_subcolumn;
        }
    }
    
    $column->priority = $priority;
    
    $board->ownColumnBoardList[] = $column;
    
    $b_board_id = R::store( $board );
    jsonSuccessDie("column_id", $column->id);
}
jsonErrorDie("Not all mandatory parameters set!");